package com.dxl.lanju.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dxl.common.annotation.RequirePermission;
import com.dxl.common.model.Page;
import com.dxl.common.model.RespData;
import com.dxl.common.utils.Utils;
import com.dxl.lanju.service.ProductService;
import com.dxl.web.common.controller.BaseController;
import com.lanju.bean.ProductSkuBean;
import com.lanju.entity.Product;
import com.lanju.entity.ProductSku;

/**
 * 产品信息控制器
 * @author 邓小林
 */
@Controller
@RequestMapping("/product")
@RequirePermission(value={"PRODUCT-MODULE"}, name="产品模块", desc="包括产品增、删、改、查")
public class ProductController extends BaseController {
	@Autowired
	private ProductService productService;
	
	@RequestMapping("")
	@RequirePermission(value={"PRODUCT-VIEW"}, name="产品查看", desc="查看产品信息")
	public ModelAndView product() {
		ModelAndView mv = new ModelAndView("product/list");
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/list")
	@RequirePermission(value={"PRODUCT-VIEW"}, name="产品查看", desc="查看产品信息")
	public Page<Product> list(String cateId, String keyword, Integer state, Integer page, Integer rows) {
		Page<Product> p = this.productService.list(cateId, keyword, state, page, rows);
		return p;
	}
	
	@RequestMapping("/edit")
	@RequirePermission(value={"PRODUCT-VIEW"}, name="产品查看", desc="查看产品信息")
	public ModelAndView edit(String id) {
		ModelAndView mv = new ModelAndView("product/edit");
		if (Utils.isNotBlank(id)) {
			Product product = this.baseService.get(Product.class, id);
			mv.addObject("entity", product);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	@RequirePermission(value={"PRODUCT-MODIFY"}, name="产品修改", desc="修改产品信息")
	public RespData save(Product product) {
		return productService.save(product);
	}
	
	@ResponseBody
	@RequestMapping("/del")
	@RequirePermission(value={"PRODUCT-DEL"}, name="产品删除", desc="删除产品信息")
	public RespData del(String[] ids) {
		return productService.del(ids);
	}
	
	@ResponseBody
	@RequestMapping("/state")
	@RequirePermission(value={"PRODUCT-STATE"}, name="产品状态修改", desc="修改产品状态")
	public RespData state(String[] ids, Integer state) {
		return productService.state(ids, state);
	}
	
	@ResponseBody
	@RequestMapping("/listSku")
	@RequirePermission(value={"PRODUCTSKU-VIEW"}, name="产品规格查看", desc="查看产品规格信息")
	public List<ProductSku> listSku(String prodId) {
		List<ProductSku> psku = this.productService.listSku(prodId);
		return psku;
	}
	
	@RequestMapping("/editSku")
	@RequirePermission(value={"PRODUCTSKU-VIEW"}, name="产品规格查看", desc="查看产品规格信息")
	public ModelAndView editSku(String prodId, String id) {
		ModelAndView mv = new ModelAndView("product/editSku");
		ProductSku productSku = new ProductSku();
		productSku.setProdId(prodId);
		if (Utils.isNotBlank(id)) {
			productSku = this.baseService.get(ProductSku.class, id);
		}
		mv.addObject("entity", productSku);
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/saveSku")
	@RequirePermission(value={"PRODUCTSKU-MODIFY"}, name="产品规格修改", desc="修改产品规格信息")
	public RespData saveSku(ProductSku product) {
		return productService.saveSku(product);
	}
	
	@ResponseBody
	@RequestMapping("/delSku")
	@RequirePermission(value={"PRODUCTSKU-DEL"}, name="产品规格删除", desc="删除产品规格信息")
	public RespData delSku(String id) {
		return productService.delSku(id);
	}
	
	@RequestMapping("/selectSku")
	@RequirePermission(value={"PRODUCTSKU-VIEW"}, name="产品规格查看", desc="查看产品规格信息")
	public ModelAndView selectSku() {
		ModelAndView mv = new ModelAndView("product/selectSku");
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/listProductSku")
	@RequirePermission(value={"PRODUCTSKU-VIEW", "PRODUCT-VIEW"}, name="产品规格查看", desc="查看产品规格信息")
	public Page<ProductSkuBean> listProductSku(String cateId, String keyword, Integer state, Integer page, Integer rows) {
		Page<ProductSkuBean> p = this.productService.listProductSku(cateId, keyword, state, page, rows);
		return p;
	}
}
